package io.github.yotta.export.web.autoconfiguration;

import io.github.yotta.export.core.export.ExcelExporterExecutor;
import io.github.yotta.export.core.export.impl.EasyExcelExporterExecutor;
import io.github.yotta.export.core.provider.FileProvider;
import io.github.yotta.export.core.provider.LocalFileProvider;
import io.github.yotta.export.core.provider.MinioFileProvider;
import io.github.yotta.export.core.provider.MinioFileProviderProperties;
import io.github.yotta.export.web.controller.DataExportController;
import io.github.yotta.export.web.controller.ExcelExportProcessor;
import io.github.yotta.export.web.iface.ExportTaskCache;
import io.github.yotta.export.web.iface.InMemoryExportTaskCache;
import io.github.yotta.export.web.manager.ExcelExportManager;
import io.github.yotta.export.web.manager.ExcelExportManagerImpl;
import io.github.yotta.export.web.manager.ExcelExporterRegister;
import io.github.yotta.export.web.manager.MapExcelExporterRegister;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import java.io.IOException;

/**
 * @author yotta
 * @date 2023/10/31 13:39
 */
@Configuration
@Import({DataExportBeanPostProcessorConfiguration.class})
@SuppressWarnings({"unused"})
@ComponentScan("io.github.yotta.export.web")
@EnableConfigurationProperties(DataExportProperties.class)
@Slf4j
public class DataExportAutoConfiguration {

    @Bean
    @ConditionalOnProperty(prefix = "yotta.data-export", name = "file-storage-type", havingValue = "local", matchIfMissing = true)
    public FileProvider localFileProvider(DataExportProperties properties) {
        log.info("use local file provider, baseDir is {}", properties.getBaseDir());
        return new LocalFileProvider(properties.getBaseDir());
    }

    @Bean
    @ConditionalOnProperty(prefix = "yotta.data-export", name = "file-storage-type", havingValue = "minio")
    public FileProvider minioFileProvider(DataExportProperties properties) {
        MinioFileProviderProperties minio = properties.getMinio();
        log.info("use minio file provider,burck is {}", minio.getBucketName());
        return new MinioFileProvider(minio);
    }

    @Bean
    @ConditionalOnMissingBean(ExcelExporterExecutor.class)
    public ExcelExporterExecutor exporterExecutor(FileProvider fileProvider) throws IOException {
        return new EasyExcelExporterExecutor(fileProvider);
    }

    @Bean
    @ConditionalOnMissingBean(ExcelExporterRegister.class)
    public ExcelExporterRegister excelExporterRegister() {
        return new MapExcelExporterRegister();
    }

    @Bean
    @ConditionalOnMissingBean(ExcelExportManager.class)
    public ExcelExportManager excelExportManager(ExcelExporterExecutor exporterExecutor,
                                                 ExcelExporterRegister excelExporterRegister
    ) {
        return new ExcelExportManagerImpl(exporterExecutor, excelExporterRegister);
    }


    @Bean
    @ConditionalOnMissingBean(ExportTaskCache.class)
    public ExportTaskCache exportTaskCache(
    ) {
        return new InMemoryExportTaskCache();
    }

    @Bean
    @ConditionalOnProperty(prefix = "yotta.data-export", name = "enable-web", havingValue = "true", matchIfMissing = true)
    public DataExportController exportController(FileProvider fileProvider, ExcelExportProcessor excelProcessor, ExportTaskCache exportTaskCache) {
        return new DataExportController(fileProvider, excelProcessor, exportTaskCache) {
        };
    }
}
